package com.zenithmind.mall.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.annotation.RequireAuth;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.mall.pojo.dto.OrderDTO;
import com.zenithmind.mall.pojo.query.OrderQuery;
import com.zenithmind.mall.pojo.vo.OrderVO;
import com.zenithmind.mall.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 订单控制器
 * 
 * @author ZenithMind Team
 * @since 2025-06-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/mall/order")
@RequiredArgsConstructor
@RequireAuth
@Tag(name = "订单管理", description = "订单相关接口")
public class OrderController {

    private final OrderService orderService;

    @PostMapping
    @Operation(summary = "创建订单", description = "创建新订单")
    @Log(description = "创建订单", operationType = OperationTypeEnum.ADD)
    public Result<String> createOrder(@Validated @RequestBody OrderDTO orderDTO) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 创建订单", userId);
        String orderId = orderService.createOrder(orderDTO, userId);
        return Result.success(orderId);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询订单列表", description = "查询当前用户的订单列表")
    public Result<PageResult<OrderVO>> getOrderPage(@Validated OrderQuery query) {
        String userId = UserContext.requireUserId();
        query.setUserId(userId); // 只能查询自己的订单
        log.info("用户 {} 查询订单列表: {}", userId, query);
        PageResult<OrderVO> result = orderService.getOrderPage(query);
        return Result.success(result);
    }

    @GetMapping("/admin/page")
    @Operation(summary = "管理员分页查询订单列表", description = "管理员查询所有订单")
    @RequireAuth(admin = true)
    public Result<PageResult<OrderVO>> getOrderPageForAdmin(@Validated OrderQuery query) {
        log.info("管理员查询订单列表: {}", query);
        PageResult<OrderVO> result = orderService.getOrderPage(query);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询订单详情", description = "根据订单ID查询详细信息")
    @Parameter(name = "id", description = "订单ID", required = true)
    public Result<OrderVO> getOrderById(@PathVariable String id) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 查询订单详情: {}", userId, id);
        OrderVO result = orderService.getOrderById(id, userId);
        return Result.success(result);
    }

    @GetMapping("/no/{orderNo}")
    @Operation(summary = "根据订单号查询订单", description = "根据订单号查询订单详情")
    @Parameter(name = "orderNo", description = "订单号", required = true)
    public Result<OrderVO> getOrderByNo(@PathVariable String orderNo) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 根据订单号查询订单: {}", userId, orderNo);
        OrderVO result = orderService.getOrderByNo(orderNo, userId);
        return Result.success(result);
    }

    @PostMapping("/{id}/pay")
    @Operation(summary = "支付订单", description = "支付指定订单")
    @Parameter(name = "id", description = "订单ID", required = true)
    @Log(description = "支付订单", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> payOrder(@PathVariable String id) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 支付订单: {}", userId, id);
        Boolean result = orderService.payOrder(id, userId);
        return Result.success(result);
    }

    @PostMapping("/{id}/cancel")
    @Operation(summary = "取消订单", description = "取消指定订单")
    @Parameter(name = "id", description = "订单ID", required = true)
    @Parameter(name = "reason", description = "取消原因", required = true)
    @Log(description = "取消订单", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> cancelOrder(@PathVariable String id, @RequestParam String reason) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 取消订单: {}, 原因: {}", userId, id, reason);
        Boolean result = orderService.cancelOrder(id, reason, userId);
        return Result.success(result);
    }

    @PostMapping("/{id}/ship")
    @Operation(summary = "发货", description = "管理员发货操作")
    @Parameter(name = "id", description = "订单ID", required = true)
    @Parameter(name = "shippingCompany", description = "物流公司", required = true)
    @Parameter(name = "shippingNo", description = "物流单号", required = true)
    @RequireAuth(admin = true)
    @Log(description = "订单发货", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> shipOrder(@PathVariable String id, 
                                   @RequestParam String shippingCompany,
                                   @RequestParam String shippingNo) {
        log.info("管理员发货订单: {}, 物流公司: {}, 物流单号: {}", id, shippingCompany, shippingNo);
        Boolean result = orderService.shipOrder(id, shippingCompany, shippingNo);
        return Result.success(result);
    }

    @PostMapping("/{id}/confirm")
    @Operation(summary = "确认收货", description = "用户确认收货")
    @Parameter(name = "id", description = "订单ID", required = true)
    @Log(description = "确认收货", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> confirmReceive(@PathVariable String id) {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 确认收货: {}", userId, id);
        Boolean result = orderService.confirmReceive(id, userId);
        return Result.success(result);
    }

    @PostMapping("/{id}/finish")
    @Operation(summary = "完成订单", description = "管理员完成订单")
    @Parameter(name = "id", description = "订单ID", required = true)
    @RequireAuth(admin = true)
    @Log(description = "完成订单", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> finishOrder(@PathVariable String id) {
        log.info("管理员完成订单: {}", id);
        Boolean result = orderService.finishOrder(id);
        return Result.success(result);
    }

    @GetMapping("/statistics")
    @Operation(summary = "查询订单统计", description = "获取当前用户的订单统计信息")
    public Result<Object> getOrderStatistics() {
        String userId = UserContext.requireUserId();
        log.info("用户 {} 查询订单统计", userId);
        Object result = orderService.getOrderStatistics(userId);
        return Result.success(result);
    }
}
